import { createRouter, createWebHistory } from 'vue-router'

import { useUserStore } from '@/store/user'

// 开启history模式
const routerHistory = createWebHistory()

const router = createRouter({
  history: routerHistory,
  routes: [
    {
      path: '/',
      name: 'Index',
      component: () => import('../views/common/Index.vue'),
      meta: {
        title: '芝士'
      }
    },
    {
      path: '/register',
      name: 'Register',
      component: () => import('../views/Entry/Register.vue'),
      meta: {
        title: '注册'
      }
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('../views/Entry/Login.vue'),
      meta: {
        title: '登录'
      }
    },
    {
      path: '/homepage',
      name: 'HomePage',
      component: () => import('../views/common/HomePage.vue'),
      meta: {
        title: '主页'
      }
    },
    {
      path: '/research/add',
      name: 'ResearchAdd',
      component: () => import('../views/Research/ResearchAdd.vue'),
      meta: {
        title: '新增研究成果'
      }
    },
    {
      path: '/profile/:id',
      component: () => import('../views/User/Index.vue'),
      children: [
        {
          path: '',
          name: 'Profile',
          props: true,
          meta: {
            title: '个人主页'
          },
          component: () => import('../views/User/Profile.vue')
        },
        {
          path: 'research',
          name: 'UserResearch',
          props: true,
          meta: {
            title: '科研成果'
          },
          component: () => import('../views/User/Research.vue')
        },
        {
          path: 'project',
          name: 'UserProject',
          props: true,
          meta: {
            title: '科研项目'
          },
          component: () => import('../views/User/Project.vue')
        },
        {
          path: 'stats',
          name: 'UserStats',
          props: true,
          meta: {
            title: '科研状态'
          },
          component: () => import('../views/User/Stats.vue')
        }
      ]
    },
    // 消息
    {
      path: '/project/add',
      name: 'ProjectAdd',
      component: () => import('../views/Project/ProjectAdd.vue'),
      meta: {
        title: '新增研究项目'
      }
    },
    {
      path: '/expert/search',
      name: 'Expert',
      component: () => import('../views/Expert/Expert.vue'),
      meta: {
        title: '搜索研究人员'
      }
    },
    {
      path: '/research/detail/:id',
      props: true,
      name: 'Research',
      component: () => import('../views/Research/Research.vue'),
      meta: {
        title: '研究成果详情'
      }
    },
    {
      path: '/message',
      component: () => import('../views/Message/Message.vue'),
      redirect: '/message/chat/0',
      children: [
        {
          path: 'chat/:target',
          component: () => import('../views/Message/components/MsgChat.vue'),
          meta: {
            title: '我的消息',
            login: true
          },
          props: true
        },
        {
          path: 'at',
          component: () => import('../views/Message/components/MsgAt.vue'),
          meta: {
            title: '@我的',
            login: true
          }
        },
        {
          path: 'reply',
          component: () => import('../views/Message/components/MsgReply.vue'),
          meta: {
            title: '回复我的',
            login: true
          }
        },
        {
          path: 'notice',
          component: () => import('../views/Message/components/MsgNotice.vue'),
          meta: {
            title: '系统通知',
            login: true
          }
        }
      ]
    },
    // 求助
    {
      path: '/forum/list/:type',
      component: () => import('@/views/Forum/ForumList.vue'),
      props: true,
      meta: {
        title: '列表-学术求助'
      }
    },
    {
      path: '/forum/create/:id',
      component: () => import('@views/Forum/ForumCreate.vue'),
      props: true,
      meta: {
        title: '创建/修改-学术求助',
        login: true
      }
    },
    {
      path: '/forum/detail/:id',
      component: () => import('@views/Forum/ForumDetail.vue'),
      props: true,
      meta: {
        title: '详情-学术求助'
      }
    },
    {
      path: '/404',
      name: '404',
      component: () => import('../views/common/404.vue'),
      meta: {
        title: '页面未找到'
      }
    },
    {
      path: '/:catchAll(.*)',
      redirect: '/404'
    }
  ]
})

router.beforeEach((to, from, next) => {
  // 切换路由时先取消所有请求
  useUserStore().clearCancelToken()
  next()
})

export default router
